var Loader = {
  Query: function()
  {
    
  },
  Add: function(src)
  {
    this.List[src] = false;
  },
  List: {},
  ItemComplete: function(event)
  {
    //console.log(this.dataset.key, 'ItemComplete', event);
    Loader.List[this.dataset.key] = true;
    
    var total = 0,
      complete = 0;
    
    for (var key in Loader.List)
    {
      total++;
      Loader.List[key] && complete++;
    }
    
    //console.log(Math.round(complete / total * 100));
    
    if (Loader.ItemCompleteCall) Loader.ItemCompleteCall.call(Loader, Math.round(complete / total * 100), total, complete);
    if (complete == total && Loader.AllCompleteCall) Loader.AllCompleteCall.call(Loader, total);
  },
  AllComplete: function(event)
  {
    console.log(this.dataset.key, 'AllComplete', event);
  },
  Run: function(itemComplete, allComplete)
  {
    if (this.IsRun) return;
    this.IsRun = true;
  
    this.ItemCompleteCall = itemComplete;
    this.AllCompleteCall = allComplete;
    
    for(var key in this.List)
    {
      var image = new Image();
      
      image.addEventListener('load', this.ItemComplete, false);
      image.addEventListener('error', this.ItemComplete, false);
      image.dataset.key = key;
      image.src = key;
    }
  }
};

export default Loader
